package com.bndg.blackjack.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
/**
 * @author r
 * @date 2024/12/10
 * @description 扑克背景view
 */

public class DiamondPatternView extends View {
    private Paint fillPaint;
    private Paint borderPaint;
    private Paint outerBorderPaint;

    public DiamondPatternView(Context context) {
        super(context);
        init();
    }

    public DiamondPatternView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DiamondPatternView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        // 背景颜色
        fillPaint = new Paint();
        fillPaint.setColor(Color.WHITE); // 设置背景为白色
        fillPaint.setStyle(Paint.Style.FILL); // 设置为填充模式

        // 边框颜色
        borderPaint = new Paint();
        borderPaint.setColor(Color.WHITE); // 边框颜色
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setStrokeWidth(3f); // 边框宽度

        // 外部边框颜色
        outerBorderPaint = new Paint();
        outerBorderPaint.setColor(Color.WHITE); // 外部边框颜色
        outerBorderPaint.setStyle(Paint.Style.STROKE);
        outerBorderPaint.setStrokeWidth(6f); // 外部边框宽度
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 绘制背景
        canvas.drawRect(0, 0, getWidth(), getHeight(), fillPaint);

        int width = getWidth();
        int height = getHeight();
        int diamondSize = 20; // 菱形的尺寸
        int gap = 4; // 菱形之间的间隔

        // 绘制菱形条纹
        for (int x = 0; x < width; x += diamondSize + gap) {
            for (int y = 0; y < height; y += diamondSize + gap) {
                drawDiamond(canvas, x, y, diamondSize);
            }
        }

        // 绘制外部边框
        canvas.drawRect(0, 0, width, height, outerBorderPaint); // 绘制外部边框
    }

    private void drawDiamond(Canvas canvas, int x, int y, int size) {
        Path path = new Path();
        path.moveTo(x + size / 2f, y); // 顶点
        path.lineTo(x + size, y + size / 2f); // 右边
        path.lineTo(x + size / 2f, y + size); // 底部
        path.lineTo(x, y + size / 2f); // 左边
        path.close(); // 闭合路径

        // 填充菱形
        canvas.drawPath(path, fillPaint); // 填充菱形
        // 绘制边框
        canvas.drawPath(path, borderPaint); // 绘制边框
    }
}
